package com.example.test.activity;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.zip.Inflater;

import com.example.test.R;
import com.example.test.R.id;
import com.example.test.R.layout;
import com.example.test.activity.PullDownView.OnPullDownListener;
import com.example.test.model.Constants;
import com.example.test.model.FormatTools;
import com.example.test.model.imgdown.AsyncDownHeadImageTask;
import com.example.test.model.imgdown.AsyncDownStatusImageTask;
import com.example.test.model.json_analyzer.HashDataProcessor;
import com.example.test.model.json_analyzer.SinaJsonAnalyzer;
import com.example.test.model.social_api.NetAPI;
import com.example.test.model.social_api.QQAPI;
import com.example.test.model.social_api.SinaAPI;
import com.tencent.weibo.sdk.android.model.GlobalVar;

import android.R.drawable;
import android.R.integer;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class FollowerListActivity extends Activity  implements OnPullDownListener{

	String uid;
	PullDownView pullDownView;
	

	ListView lvUser;
	BaseAdapter adapter;
	int sIndex=0;
	List<HashMap<String, String>> lArrUserList=null;
	List<HashMap<String, String>> temList=null;
	HashMap<String, byte[]>  mapImgList=new HashMap<String, byte[]>();
	public Handler handler=new Handler(){

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case Constants.MSG_GETFOLLOWERS_FAIL:
				Toast.makeText(FollowerListActivity.this, "获取关注列表失败", 1000).show();
				FollowerListActivity.this.finish();
				break;
			case Constants.MSG_GETFOLLOWERS_SUCCESS:
				pullDownView.notifyDidLoad();
				pullDownView.notifyDidRefresh();
				pullDownView.notifyDidMore();
				temList=(List<HashMap<String, String>>)msg.obj;
				setRealUI();	
				break;
			case Constants.MSG_CANCELFOLLOW_SUCCESS:
				Toast.makeText(FollowerListActivity.this, " 取消关注成功", 1000).show();
				for (HashMap<String, String> map : lArrUserList) {
					String id=map.get("uid");
					String uid=msg.obj+"";
					if(id.equals(uid))
					{
						map.put("following", "false");
						adapter.notifyDataSetChanged();

						break;
					}
				}
				break;
			case Constants.MSG_FOLLOW_SUCCESS:
				Toast.makeText(FollowerListActivity.this, " 成功关注", 1000).show();
				for (HashMap<String, String> map : lArrUserList) {
					String id=map.get("uid");
					String uid=msg.obj+"";
					if(id.equals(uid))
					{
						map.put("following", "true");
						adapter.notifyDataSetChanged();

						break;
					}
				}
				break;
			case Constants.MSG_CANCELFOLLOW_FAIL:
				Toast.makeText(FollowerListActivity.this, " 取消关注失败：失败码："+msg.obj, 1000).show();
				break;
			case Constants.MSG_DOWNSTATUSIMG_SUCCESS:
				HashMap<String, Object> map=(HashMap<String, Object>)msg.obj;
				mapImgList.put((String)map.get("id"),(byte[]) map.get("img"));
				adapter.notifyDataSetChanged();
			default:
				break;
			}
		}
		
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_fris);
		pullDownView=(PullDownView)findViewById(R.id.pull_down_view);
		pullDownView.setOnPullDownListener(this);
		lvUser=pullDownView.getListView();
		Intent intent=getIntent();
		Bundle bundle = intent.getExtras();
		uid=(String)bundle.get("uid");
		setUI();
	}
    
	
	@Override
	public void onRefresh() {
		if(GlobalVar.enWhich.equals("sina"))
		SinaAPI.getFollowersList(handler, uid,0);
		else if(GlobalVar.enWhich.equals("QQ"))
		QQAPI.getUserFollowers(this, "", 50, 0, handler);
		else if(GlobalVar.enWhich.equals("netease"))
		NetAPI.getFollowers("", null, 0, handler);
		sIndex=0;
		lArrUserList.clear();
	}

	@Override
	public void onMore() {
		sIndex+=50;
		if(GlobalVar.enWhich.equals("sina"))
		SinaAPI.getFollowersList(handler, uid,sIndex);
		else if(GlobalVar.enWhich.equals("QQ"))
		QQAPI.getUserFollowers(this, "", 50, sIndex, handler);
		else if(GlobalVar.enWhich.equals("netease"))
		NetAPI.getFollowers("", null, sIndex, handler);
		
	}
	public void setUI()
	{
		if(GlobalVar.enWhich.equals("sina"))
		SinaAPI.getFollowersList(handler, uid,0);
		else if(GlobalVar.enWhich.equals("QQ"))
		{
			QQAPI.getUserFollowers(this, "", 50, 0, handler);
		}
		else if(GlobalVar.enWhich.equals("netease"))
		{
			NetAPI.getFollowers("", null, 0, handler);
		}
		
	}
	
	public void setRealUI()
	{
		int i=0;
		if(temList==null)
			return;
		for (HashMap<String, String> map : temList) {
			if(!mapImgList.containsKey(map.get("uid")))
			{
			String imgUrl = map.get("profile_image_url");
			mapImgList.put(map.get("uid"), null);
			if(imgUrl.equals(""))
			{
			 continue;	
			}
			Thread thread=new Thread(new AsyncDownStatusImageTask(handler, map.get("uid"),imgUrl));
			thread.start();
			}
			i++;
		}
		if(lArrUserList==null)
		{
			lArrUserList=temList;
			adapter=new FollowersListAdapter(this, lArrUserList,mapImgList);
			lvUser.setAdapter(adapter);
		}
		else
		{
			lArrUserList.addAll(temList);
			adapter.notifyDataSetChanged();
		}
		
	}
	 
 }

class FollowersListAdapter extends BaseAdapter
{
	List<HashMap<String, String>> data;
	Context context;
	HashMap<String, byte[]> mapImgList;
	public FollowersListAdapter(Context context,List<HashMap<String, String>> data,HashMap<String, byte[]> mapImgList)
	{
		this.context=context;
		this.data=data;
		this.mapImgList=mapImgList;
	}
	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup arg2) {
		if(convertView==null)
		{
		   convertView = LayoutInflater.from(context).inflate(R.layout.fris_item,null);
		}
		TextView tvUserName =(TextView)convertView.findViewById(R.id.tvUserName);
		TextView tvUserDsr =(TextView)convertView.findViewById(R.id.tvUserDsr);
		Button btFollow =(Button)convertView.findViewById(R.id.btFollow);
		if(data.get(position).get("following").equals("false"))
			btFollow.setText("关注");
		else
			btFollow.setText("取消关注");
		tvUserName.setText(data.get(position).get("name"));
		tvUserDsr.setText(data.get(position).get("description"));
		RelativeLayout  rlFris=(RelativeLayout)convertView.findViewById(R.id.rlFris);
        rlFris.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
			  Intent intent=new Intent();
			  intent.putExtra("uid", data.get(position).get("uid"));
			  intent.putExtra("activity_type", data.get(position).get("resource"));
			  intent.setClass(context,HomeActivity.class);
			  context.startActivity(intent);
				
			}
		});
		btFollow.setOnClickListener(new Button.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Button view=(Button)arg0;
				if(view.getText().equals("取消关注"))
				{
				if(GlobalVar.enWhich.equals("sina"))
				SinaAPI.cancelFollow(((FollowerListActivity)context).handler ,data.get(position).get("uid"),data.get(position).get("name") );
				else if(GlobalVar.enWhich.equals("QQ"))
				QQAPI.cancelFollow(context, data.get(position).get("uid"), ((FollowerListActivity)context).handler);
				else if(GlobalVar.enWhich.equals("netease"))
				NetAPI.cancelFollow(data.get(position).get("uid"), ((FollowerListActivity)context).handler);
				}
				else 
				{
					    if(GlobalVar.enWhich.equals("sina"))
						SinaAPI.follow(((FollowerListActivity)context).handler ,data.get(position).get("uid") );
						else if(GlobalVar.enWhich.equals("QQ"))
						QQAPI.follow(context, data.get(position).get("uid"), ((FollowerListActivity)context).handler);
						else if(GlobalVar.enWhich.equals("netease"))
						NetAPI.follow(data.get(position).get("uid"), ((FollowerListActivity)context).handler);
				}
			}
		});
		if(mapImgList.get(data.get(position).get("uid"))!=null)
		{
			ImageView ivUserHead =(ImageView)convertView.findViewById(R.id.ivUserHead);
			ivUserHead.setImageDrawable(FormatTools.getInstance().Bytes2Drawable(mapImgList.get(data.get(position).get("uid"))));
					
					
		}
		else
		{
			ImageView ivUserHead =(ImageView)convertView.findViewById(R.id.ivUserHead);
			ivUserHead.setImageResource(R.drawable.nohead);
		}
		return convertView;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}
	
}